#!/bin/bash

# GitHub 路径
GitHub_push="https://github.com/Glinfeny/Tauri-Admin-Template-build"
# gitee 路径
Gitee_push="https://gitee.com/gao-ling-feng/tauri-admin-template--download"

# 定义JSON文件和输出文件的位置
json_file="update/data.json"
output_file="README_LOG.md"


# 使用sed命令替换URL
sed -i '' 's|'"$GitHub_push"'|'"$Gitee_push"'|g' "$json_file"

# 使用jq读取version值
version=$(jq -r '.version' "$json_file")
# commir
notes=$(jq -r '.notes' "$json_file")

# 检查version是否获取成功
if [ -z "$version" ]; then
    echo "Version not found in JSON file"
    exit 1
fi


# 创建Markdown格式的更新日志
{
    echo "## v$version" # 添加二级标题
    echo ""
    echo "### Release notes:"
    jq -r '.notes' "$json_file"
    echo ""
    echo "### Published Date:"
    jq -r '.pub_date' "$json_file"
    echo ""
    echo "### Platforms:"
    jq -r '.platforms | to_entries[] | "- [\(.key)](\(.value.url))"' "$json_file"
    echo ""
    # 使用cat命令将新内容和原始内容合并，然后将结果覆盖到output_file
} | cat - "$output_file" > temp_output_file && mv temp_output_file "$output_file"


# push
git add .
git commit -m "$notes"
git push origin

# 执行git tag命令
git tag "v$version"
git push origin "v$version"

# 输出信息
echo "Git tag v$version created successfully"
